<?php

/**
 * HTTP响应类
 *
 * @category   ORG
 * @package    ORG
 * @subpackage Response
 * @author     海啸 <459103503@qq.com>
 */
class JsonResponse extends HttpResponse
{
    const RESPONSE_TYPE = 'application/json';

    /**
     * construct
     *
     * @param string $protocol    protocol
     * @param string $charset     charset
     * @param string $contentType contentType
     */
    public function __construct($protocol = 'HTTP/1.1',
        $charset = HttpResponse::DEFAULT_CHARSET, $contentType = ''
    ) {
        parent::__construct($protocol, $charset, self::RESPONSE_TYPE);
    }

    /**
     * 创建一个正确的JsonResponse,状态值默认是200
     *
     * @param mixed   $bodyValue response body，一个可以json_encode的对象,或数组
     * @param integer $code      http code
     *
     * @return JsonResponse
     */
    public function createJsonResponse($bodyValue = array(), $code = self::HTTP_OK)
    {

        $result = array(
            'status' => array(
                'code' =>$code,
                'msg'  => 'OK'
            ),
            'data' =>$bodyValue
        );

        return $this->setStatusCode($code)
            ->setBody($result)
            ->addHeader('Access-Control-Allow-Credentials', 'true')
            ->addHeader('Access-Control-Allow-Headers', 'Accept, Origin, X-Requested-With, Content-Type')
            ->addHeader('Access-Control-Allow-Methods', 'POST')
            ->addHeader('Access-Control-Allow-Methods', 'GET')
            ->addHeader('Access-Control-Allow-Methods', 'OPTIONS')
            ->addHeader('Access-Control-Allow-Methods', 'PUT')
            ->addHeader('Access-Control-Allow-Methods', 'DELETE')
            ->addHeader('Access-Control-Allow-Origin', '*')
            ->addHeader('Allow', 'PUT')
            ->addHeader('Allow', 'OPTIONS')
            ->addHeader('Allow', 'DELETE')
            ->addHeader('Allow', 'POST')
            ->addHeader('Allow', 'GET')
            ->setHeaders()
            ->getBody();
    }

    /**
     * 创建一个错误的JsonResponse,状态值默认是400
     *
     * @param string  $message error message
     * @param integer $code    error code httpCode
     *
     * @return JsonResponse
     */
    public function createErrorJsonResponse($message = 'Error', $code = self::HTTP_BAD_REQUEST)
    {
        $result = array(
            'status' => array(
                'code' =>$code,
                'msg'  => $message
            ),
            'data' =>array()
        );

        return $this->setStatusCode($code)->setBody($result)
            ->addHeader('Error-Message', $message)
            ->addHeader('Access-Control-Allow-Credentials', 'true')
            ->addHeader('Access-Control-Allow-Headers', 'Accept, Origin, X-Requested-With, Content-Type')
            ->addHeader('Access-Control-Allow-Methods', 'POST')
            ->addHeader('Access-Control-Allow-Methods', 'GET')
            ->addHeader('Access-Control-Allow-Methods', 'OPTIONS')
            ->addHeader('Access-Control-Allow-Methods', 'PUT')
            ->addHeader('Access-Control-Allow-Methods', 'DELETE')
            ->addHeader('Access-Control-Allow-Origin', '*')
            ->addHeader('Allow', 'PUT')
            ->addHeader('Allow', 'OPTIONS')
            ->addHeader('Allow', 'DELETE')
            ->addHeader('Allow', 'POST')
            ->addHeader('Allow', 'GET')
            ->setHeaders()
            ->getBody();
    }
}
